package com.l024.easysecurity.modules.sys.service.impl;

import com.l024.easysecurity.common.exception.RRException;
import com.l024.easysecurity.common.page.PageData;
import com.l024.easysecurity.common.page.PageParams;
import com.l024.easysecurity.common.utils.R;
import com.l024.easysecurity.modules.sys.dao.SysMenuMapper;
import com.l024.easysecurity.modules.sys.dao.SysRoleMapper;
import com.l024.easysecurity.modules.sys.dao.SysUserMapper;
import com.l024.easysecurity.modules.sys.entity.SysMenu;
import com.l024.easysecurity.modules.sys.entity.SysRole;
import com.l024.easysecurity.modules.sys.entity.SysUser;
import com.l024.easysecurity.modules.sys.service.SysUserService;
import com.l024.easysecurity.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户相关
 */
@Service
public class SysUserServiceImpl extends SysBaseService implements SysUserService {
    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysMenuMapper sysMenuMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Override
    public SysUser findSysUserByUserName(String userName) {
        if(!StringUtils.isEmpty(userName)){
            return sysUserMapper.findUserByUserName(userName);
        }
        return null;
    }

    /**
     * 获取用户的所有菜单信息，进行封装
     * @param userId
     * @return
     */
    @Override
    public List<SysMenu> getUserAllMenus(long userId) {
        if(userId>0){
            List<SysMenu> sysMenus = sysMenuMapper.queryUserAllMenus(userId);
            return getTreeMenu(sysMenus);
        }
        return null;
    }

    /**
     * 分页查询条件查询
     * @param sysUser
     * @param pageParam
     * @return
     */
    @Override
    public PageData<SysUser> selectPageSysUser(SysUser sysUser, PageParams pageParam) {
        List<SysUser> sysUsers = sysUserMapper.selectPageSysUser(sysUser, pageParam);
        Long count = sysUserMapper.selectPageCount(sysUser);
        PageData<SysUser> pageData =PageData.buildPage(sysUsers,count,pageParam.getPageSize(),pageParam.getPageNum());
        return pageData;
    }

    /**
     * 添加数据
     * @param sysUser
     * @return
     */
    @Transactional
    @Override
    public R addUser(SysUser sysUser) {
        if(sysUser==null){
            return R.error("用户信息不可为null");
        }
        if(StringUtils.isEmpty(sysUser.getUsername())){
            return R.error("账户不可为null");
        }
        if(StringUtils.isEmpty(sysUser.getPassword())){
            return R.error("密码不可为null");
        }

        if(StringUtils.isEmpty(sysUser.getMobile())){
            return R.error("手机号不可为null");
        }
        if(StringUtils.isEmpty(sysUser.getEmail())){
            return R.error("邮箱不可为null");
        }
        long roleId = sysUser.getRoleId();
        if(roleId<1){
            return R.error("角色信息不存在");
        }
        //判断添加的用户是否存在
        SysUser su = new SysUser();
        su.setUsername(sysUser.getUsername());
        Long count = sysUserMapper.existUser(su);
        if(count>0){
            return R.error("该用户已经存在");
        }
        //判断角色信息是否存在
        SysRole sysRole = sysRoleMapper.getSysRoleById(roleId);
        if(sysRole==null){
            return R.error("角色信息不存在");
        }
        //添加用户
        String random = RandomStringUtils.randomAlphabetic(5);
        sysUser.setSalt(random);
        //加密密码
        String s = ShiroUtils.sha256(sysUser.getPassword(), sysUser.getSalt());
        sysUser.setPassword(s);
        sysUserMapper.addUser(sysUser);
        //根据添加的用户过去id
        SysUser result = sysUserMapper.findUserByUserName(sysUser.getUsername());
        //添加角色信息
        if(result==null){
            throw new RRException("添加失败");
        }
        sysRoleMapper.addUserRole(result.getUserId(),roleId);
        return R.ok("添加成功");
    }

    @Transactional
    @Override
    public R updateUser(SysUser sysUser) {
        if(sysUser==null){
            return R.error("用户信息不可为null");
        }
        if(StringUtils.isEmpty(sysUser.getUsername())){
            return R.error("账户不可为null");
        }
        if(StringUtils.isEmpty(sysUser.getMobile())){
            return R.error("手机号不可为null");
        }
        if(StringUtils.isEmpty(sysUser.getEmail())){
            return R.error("邮箱不可为null");
        }
        long roleId = sysUser.getRoleId();
        if(roleId<1){
            return R.error("角色信息不存在");
        }
        Long aLong = sysUserMapper.updateUser(sysUser);
        //更新用户角色
        sysRoleMapper.updateUserRoleByUserId(sysUser.getUserId(),sysUser.getRoleId());
        if(aLong>0){
            return R.ok("更新成功");
        }
        return R.error("更新失败");
    }

    @Transactional
    @Override
    public R delSysUser(long userId) {
        if(userId>0){
            sysUserMapper.delSysUser(userId);
            sysRoleMapper.delSysUserRole(userId);
            return R.ok("删除成功");
        }
        return R.error("用户id不存在");
    }
}
